/* Copyright 2008. All rights reserved. */
package org.bling.parser.tags;

import org.bling.parser.BlingParserException;
import org.bling.parser.expressions.ExpressionManager;
import org.bling.parser.style.StyleParser;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Widget;

/**
 * Renders an SWT TableColumn, to be used in an SWT Table
 * 
 * @author DISBROB
 * 
 */
public class ColumnTag extends Tag {

	protected TableColumn column;
	protected String title;
	protected String property;
	
	public void setTitle(String title) {
		this.title = title;
	}
	
	public void setProperty(String property) {
		this.property = property;
	}
	
	@Override
	public int doStartTag() throws BlingParserException {
		super.doStartTag();

		if (!(parent.getWidget() instanceof Table)) {
			throw new BlingParserException(
					"Column tags can only be inside of Tables");
		}

		String evalTitle = ExpressionManager.evaluate(title, context).toString();
		String evalProperty = ExpressionManager.evaluate(property, context).toString();
		
		Table table = (Table) parent.getWidget();
		column = new TableColumn(table, SWT.BORDER | SWT.V_SCROLL);
		column.setText(evalTitle);
		column.setData(evalProperty);
		column.setAlignment(SWT.LEFT);
		column.setWidth(80); // FIXME: Make this a variable
		
		return EVAL_PAGE;
	}

	@Override
	public Widget getWidget() throws BlingParserException {
		return column;
	}

}
